home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17992 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  107 lines

  1. Newsgroups: comp.lang.c++
  2. Path: in1.uu.net!world!bgw
  3. From: bgw@world.std.com (Bruce G Wilson)
  4. Subject: g++/templates: duplicate symbols
  5. Message-ID: <Dq275z.A2t@world.std.com>
  6. Organization: The World Public Access UNIX, Brookline, MA
  7. Date: Thu, 18 Apr 1996 13:08:22 GMT
  8.  
  9.  
  10. I have a problem compiling some C++ template code under g++.  I'm getting
  11. duplicate definitions of symbols in the link stage.  The problem appears
  12. involve a class that uses two template instantiations in it.  I'm
  13. instantiating each template class in a separate file, but because I'm
  14. including the .h file for the containing class in each file, I'm getting
  15. *both* template instantiations in *each* file, which creates duplicate
  16. symbol definitions at link time.
  17.  
  18. Here's a sketch of what the code is like:
  19.  
  20. // foo.h
  21. class A {
  22. // ...
  23. private:
  24.   LinkedList<String>    _list;
  25. };
  26.  
  27. class B {
  28. // ...
  29. private:
  30.   Dictionary<String, A> _dict;
  31. };
  32.  
  33.  
  34. // foo.cpp
  35. (implementations of methods from classes A and B)
  36.  
  37.  
  38. // LinkedListString.cpp
  39. #include "String.h"
  40. #include "LinkedList.h"
  41. #include "LinkedList.cpp"
  42.  
  43. #include "foo.h"
  44.  
  45. template class LinkedList<String>;
  46.  
  47.  
  48. // DictionaryStringA.cpp
  49. #include "String.h"
  50. #include "Dictionary.h"
  51. #include "Dictionary.cpp"
  52.  
  53. #include "foo.h"
  54.  
  55. template class Dictionary<String, A>;
  56.  
  57.  
  58. foo.cpp is compiled with "-fno-implicit-templates".
  59.  
  60. The files that instantiate the template classes (LinkedListString.cpp
  61. and DictionaryStringA.cpp) are compiled without the "-fno-implicit-templates"
  62. option.  When I link, I get duplicate symbol definitions for some of
  63. the template class methods.
  64.  
  65. The duplicate symbols appear to be arising because "foo.h" is included
  66. in the two files that instantiate templates.  The classes in foo.h define
  67. two template instantiations, and both are instantiated in each of the
  68. files.  So, in other words, I get one instantiation of the code for
  69. "LinkedList<String>" and "Dictionary<String, A>" in each of the files
  70. LinkedListString.o and DictionaryStringA.o .
  71.  
  72. The workaround is to put all template instantiations for foo.h in one
  73. template instantiation file:
  74.  
  75. AllTemplates.cpp:
  76.  
  77. #include "String.h"
  78. #include "LinkedList.h"
  79. #include "LinkedList.cpp"
  80. #include "Dictionary.h"
  81. #include "Dictionary.cpp"
  82.  
  83. #include "foo.h"
  84.  
  85. template class LinkedList<String>;
  86. template class Dictionary<String, A>;
  87.  
  88.  
  89. This works, but it's not a neat solution because LinkedList<String> is
  90. a composition of two generic types which might eventually be instantiated
  91. elsewhere in the code; I'd like to be able to place template instantiations
  92. like these in a common area where they're built once and can be used in
  93. many places.  Otherwise, the next time someone uses a LinkedList<String>
  94. in their code, there will be duplicate symbol definitions all over again.
  95.  
  96. I seem to recall that other C++ compilers resolve this problem by using
  97. a special "pre-link" phase that resolves redundant template instantiations
  98. like this.  Does g++ not do this ?  Does it just call the standard 'ld'
  99. linker ?  Is there an alternate linker that comes with g++ that I could call
  100. which would handle template instantiation correctly ?
  101.  
  102. I've seen reference in some of the online documentation for g++ about a
  103. "template repository" feature that will be part of a future release of
  104. g++.  Will this feature handle the kind of problems I'm having ?
  105.  
  106. I'm working with g++ 2.6.3 on BSDI (ix86 Unix).
  107.